package com.sinosoft.dsp.component.bpm.viewbean;

import org.jbpm.xsdobject.Process;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.chart.flow.FlowChartModel;

import com.sinosoft.dsp.component.bpm.exception.SinoBPMException;
import com.sinosoft.dsp.component.bpm.util.ProcessParse;
import com.sinosoft.dsp.platform.components.AbstractViewBean;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.to.IGenericTO;
import com.sinosoft.dsp.system.to.attribute.Attribute;
import com.sinosoft.dsp.system.to.helper.ToHelper;
import com.sinosoft.dsp.system.utils.Const;

public class BpmJpdlDefineViewBean extends AbstractViewBean implements
		IBpmDefineViewBean {

	private String deploymentId;

	private String resourceName;

	private Process process;

	private FlowChartModel flowModel;

	private String pageCanvasString;

	private static final Log log = Log.getLog(BpmJpdlDefineViewBean.class
			.getName());

	@Override
	public void init(IGenericTO tos) throws SinoException {
		IBpmConfigViewBean dirViewBean = (IBpmConfigViewBean) this.getParent();
		Attribute<?> sIdAttr = ToHelper.getContentAttriubteByName(tos,
				"selectedId");

		if (sIdAttr != null && !Const.isEmpty(sIdAttr.convertToString())) {
			DefaultTreeNode tn = dirViewBean.getNode(sIdAttr.convertToString());
			BpmConfigDataObject bdo = (BpmConfigDataObject) tn.getData();

			this.deploymentId = bdo.getDeploymentId();
			this.resourceName = bdo.getResourceName();

			initBPM(bdo.getDeploymentId(), bdo.getResourceName());
		} else {
			Attribute<?> dIdAttr = ToHelper.getContentAttriubteByName(tos,
					"deploymentId");
			Attribute<?> snAttr = ToHelper.getContentAttriubteByName(tos,
					"resourceName");
			this.deploymentId = dIdAttr.convertToString();
			this.resourceName = snAttr.convertToString();
			
			createBPM();
		}
	}

	@Override
	public FlowChartModel getFlowModel() {
		try {
			if (flowModel == null) {
				flowModel = ProcessParse.processToCanvasObject(process,
						"{viewBeanId:'" + this.getId() + "'}");
			}
			return flowModel;
		} catch (SinoBPMException e) {
			log.error("构造BPM图形出现错误");
		}
		return null;
	}

	/**
	 * 通过ID初始化BPM
	 */
	private void initBPM(String deploymentId, String resourceName) {
		process = ProcessParse.parseJpdlDefine(deploymentId, resourceName);
	}

	/**
	 * 创建一个新的BPM
	 */
	private void createBPM() {
		process = new Process();
	}

	/**
	 * 获得流程对象
	 * 
	 * @return
	 */
	public Process getProcess() {
		return process;
	}

	public String getPageCanvasString() {
		return pageCanvasString;
	}

	public void setPageCanvasString(String pageCanvasString) {
		this.pageCanvasString = pageCanvasString;
	}

	public String getDeploymentId() {
		return deploymentId;
	}

	public void setDeploymentId(String deploymentId) {
		this.deploymentId = deploymentId;
	}

	public String getResourceName() {
		return resourceName;
	}

	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}

}
